﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Profile;

using MP.Core.Authentication;
using MP.Core.Authentication.Entity;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using MP.Core.Library;
using MP.Core.Config;

public partial class Member_Control_ListMember : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsShow())
        {
            LoadData();
            if (!IsPostBack)
            {
                loadRole();
                loadCombo();
            }
        }
        else
        {
            this.Visible = false;
        }
    }

    public string branchId
    {
        get { return Request["b"]; }
    }

    public string departId
    {
        get { return Request["d"]; }
    }

    public string users
    {
        get { return Request["u"]; }
    }
    public string agentnumber
    {
        get { return Request["a"]; }
    }

    void LoadData()
    {
        DataTable dt = AspnetUsersManager.CreateInstant().GetAll(branchId, departId, users, agentnumber);
        if (dt.Rows.Count > 0)
        {
            smartPager.Visible = true;
            PagedDataSource pagedata = new PagedDataSource();
            string url = "/Member/ListMember.aspx?";
            string param = "b=" + branchId + "&d=" + departId + "&u=" + Server.UrlEncode(users) + "&a=" + Server.UrlEncode(agentnumber);
            pagedata = Func.GetPaging(dt, Request["page"], GlobalConfig.SYS_RECORDS_PER_PAGE);

            this.smartPager.RowCount = pagedata.DataSourceCount;
            this.smartPager.CurrentPage = pagedata.CurrentPageIndex + 1;
            this.smartPager.PageSize = pagedata.PageSize;
            this.smartPager.Visible = this.smartPager.RowCount > this.smartPager.PageSize;
            this.smartPager.UrlFormatString = url + param + "&page={0}";
            repMember.DataSource = pagedata;
            repMember.DataBind();
        }
    }

    void loadRole()
    {
        DataTable roles = MPRoleProvider.CreateInstant().GetAll();
        roles.DefaultView.RowFilter = "IsEnable <> 0";
        chklstRoles.DataTextField = "RoleName";
        chklstRoles.DataValueField = "RoleName";
        chklstRoles.DataSource = roles.DefaultView;
        chklstRoles.DataBind();
    }

    void loadCombo()
    {
        ddlBranch.DataTextField = "BranhName";
        ddlBranch.DataValueField = "Id";
        ddlBranch.DataSource = HrBranchManager.CreateInstant().GetAllBranch();
        ddlBranch.DataBind();

        ddlDepart.DataTextField = "DepartName";
        ddlDepart.DataValueField = "Id";
        ddlDepart.DataSource = MaDepartmentManager.CreateInstant().GetAllDepart();
        ddlDepart.DataBind();

        if (!string.IsNullOrEmpty(branchId) && Util.IsGuid(branchId))
            ddlBranch.SelectedValue = branchId;
        if (!string.IsNullOrEmpty(departId) && Util.IsGuid(departId))
            ddlDepart.SelectedValue = departId;
    }

    public string getImageActive(bool IsApproved)
    {

        string st = "";
        if (IsApproved)
            st = "user-online";
        else
            st = "user-disable";
        return st;
    }

    public string getImageLock(bool IsApproved)
    {

        string st = "";
        if (!IsApproved)
            st = "user-online";
        else
            st = "user-disable";
        return st;
    }

    public string getImageOnline(bool IsOnline)
    {
        string st = "";
        if (IsOnline)
            st = "user-online.png";
        else
            st = "user-offline.png";
        return st;
    }

    protected void repMember_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string username = e.CommandArgument.ToString();
        MembershipUser user = Membership.GetUser(username);
        switch (e.CommandName)
        {
            case "enable":
                user.IsApproved = !user.IsApproved;
                Membership.UpdateUser(user);
                break;
            case "lock":
                user.UnlockUser();
                Membership.UpdateUser(user);
                break;
            case "edit":
                break;
            case "del":
                
                Membership.DeleteUser(username);
                HrPositionEntity PosObj = HrPositionManager.CreateInstant().GetByUserID(username);
                if (PosObj != null)
                {
                    PosObj.IsDeleted = true;
                    PosObj.UpdatedBy = Util.CurrentUserName;
                    PosObj.UpdatedDate = DateTime.Now;
                    HrPositionManager.CreateInstant().Update(PosObj);
                }
                HrEmployeeEntity EmObj = HrEmployeeManager.CreateInstant().GetByWindowUser(username);
                if (EmObj != null)
                {
                    EmObj.IsDeleted = true;
                    EmObj.UpdatedBy = Util.CurrentUserName;
                    EmObj.UpdatedDate = DateTime.Now;
                    HrEmployeeManager.CreateInstant().Update(EmObj);
                }
               
                break;
        }
        Response.Redirect(Request.RawUrl);
    }

    protected void btnAddRole_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string[] users = hidaddrole.Value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (ListItem item in chklstRoles.Items)
            {
                if (item.Selected)
                {
                    for (int i = 0; i < users.Length; i++)
                        Roles.AddUserToRole(users[i], item.Value);
                }

            }

            //Response.Redirect(Request.RawUrl);
        }
    }

    protected void btnCancelRole_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Member/ListMember.aspx?" +
                          "b=" + ddlBranch.SelectedValue +
                          "&d=" + ddlDepart.SelectedValue +
                          "&u=" + Server.UrlEncode(txtUser.Text.Trim()) +
                          "&a=" + Server.UrlEncode(txtAgentNumber.Text.Trim()));
    }

    public int pageindex()
    {
        int p = 0;

        if (!string.IsNullOrEmpty(Request["page"]))
            int.TryParse(Request["page"], out p);
        if (p <= 0)
            p = 1;
        return (p - 1) * GlobalConfig.SYS_RECORDS_PER_PAGE;
    }
}
